package dper.study.codes.common.simulation4jvmstudy;

import org.apache.log4j.Logger;

import java.util.*;

/**
 * Created by leichen on 2017/5/17.
 */
public class BigMemeryCacheSimulation {

    private static Logger logger = Logger.getLogger(BigMemeryCacheSimulation.class) ;

    private static List<Shop> cache;
    private static List<Shop> permCache;

    static {
        permCache = new ArrayList<Shop>();
        for(int i = 0; i < 2000000; i++){
            Shop shop = new Shop();
            shop.setId(i);
            permCache.add(shop);
        }
        System.out.println("init data");

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                cache = new ArrayList<Shop>();
                //200000 is ok, while 400000 is bad.
                for(int i = 0; i < 400000; i++){
                    Shop shop = new Shop();
                    shop.setId(i);
                    cache.add(shop);
                }
                System.out.println("refresh data");
            }
        }, 1000, 100);
    }

    //-XX:+PrintGCDetails -XX:+UseConcMarkSweepGC -XX:+ScavengeBeforeFullGC -XX:+UseParNewGC -Xms256M -Xmx256M -Xmn152M -XX:CMSInitiatingOccupancyFraction=60 -XX:SurvivorRatio=22

    public void testVeryBigMemery(){
        Map<String,String> map = new HashMap<String, String>() ;
        System.out.println("Stop");
        //模拟400w商户 内存缓存
        // 主要问题在 resize 的时候 会废弃之前的map 生成新map
        for(int i=0;i<4000000;i++){
            map.put("prefixStr_" + i , "valueInfo_"+ i) ;
            logger.info(i);
        }
    }

    public static void main(String[] argv){
        System.out.println("Start");
        try {
            Thread.sleep(600000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
